using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    public partial class FireZoneGroupEntry : BasePage_SOM
    {
        /// <summary>
        /// Author:SQB
        /// </summary>
        /// 

        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140809";
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            this.TracePageChange = true;
            if (!IsPostBack)
            {
                ViewState["FireLinkID"] = string.Empty;
                SetLanguage();
                //SetPageLoad(); //move tp  [SetLanguage], huangyao 20081208 fixed issue 0785
            }

        }

        private void SetPageLoad()
        {
            if (this.Request["FireZoneGrpID"] != null)
            {
                DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDFireZoneGrp.GetFireZoneGroupKey(int.Parse(this.Request["FireZoneGrpID"].ToString()));
                this.hidID.Value = dt.Rows[0]["FireZoneGrpID"].ToString();

                //this.ddlFireLink.SelectedValue = dt.Rows[0]["FireLinkID"].ToString(); //Del by zengshunbo 2009-02-27 TD:1218
                FindByValueAndSelect(this.ddlFireLink,dt.Rows[0]["FireLinkID"].ToString());//Add by zengshunbo 2009-02-27 TD:1218

                this.txtZoneGroup.Text = dt.Rows[0]["FireZoneGrpCode"].ToString();
                this.txtDetailLocation.Text = dt.Rows[0]["DetailLocation"].ToString();

                this.txtRowNo.Text = dt.Rows[0]["RowNo"].ToString();
                this.txtSeqNo.Text = dt.Rows[0]["SeqNo"].ToString();

                if (dt.Rows[0]["EffectiveFrom"].ToString() != String.Empty)
                {
                    this.dtbEffectiveFrom.SetDateTime(DateTime.Parse(dt.Rows[0]["EffectiveFrom"].ToString()));
                }
                else
                {
                    this.dtbEffectiveFrom.SetDateTime(DateTime.MinValue);
                }

                if (dt.Rows[0]["EffectiveTo"].ToString() != String.Empty)
                {
                    this.dtbEffectiveTo.SetDateTime(DateTime.Parse(dt.Rows[0]["EffectiveTo"].ToString()));
                }
                else
                {
                    this.dtbEffectiveTo.SetDateTime(DateTime.MinValue);
                }

                this.chkVoid.Checked = bool.Parse(dt.Rows[0]["FlagObsolete"].ToString());
                this.ddlFireLink.Enabled = false;

                if (this.chkVoid.Checked == true)
                {
                    AfterVoid();
                }
                dt.Dispose();
                dt = null;
            }
            

            this.btnSave.Attributes.Add("onclick", "return CheckData('" + 
                                                                            this.GetMessage("SOM_FireLinkValidation") + "','" + 
                                                                            this.GetMessage("SOM_FireZoneGroupValidation") + "','" +
                                                                            this.GetMessage("SOM_RowNoValidation") + "','" + 
                                                                            this.GetMessage("SOM_SeqNoValidation") + "','" + 
                                                                            //this.GetMessage("SOM_RowNoValidationNumber") + "','" + 
                                                                            //this.GetMessage("SOM_SeqNoValidationNumber") + "','" + 
                                                                            this.GetMessage("SOM_EffectiveFromValidation") + "','" +
                                                                            this.GetMessage("SOM_EffectiveToValidation") + "','" + 
                                                                            this.GetMessage("SOM_VoidSure") + "')");
            this.btnSaveAndNew.Attributes.Add("onclick", "return CheckData('" + 
                                                                            this.GetMessage("SOM_FireLinkValidation") + "','" +
                                                                            this.GetMessage("SOM_FireZoneGroupValidation") + "','" +
                                                                            this.GetMessage("SOM_RowNoValidation") + "','" + 
                                                                            this.GetMessage("SOM_SeqNoValidation") + "','" + 
                                                                            //this.GetMessage("SOM_RowNoValidationNumber") + "','" + 
                                                                            //this.GetMessage("SOM_SeqNoValidationNumber") + "','" + 
                                                                            this.GetMessage("SOM_EffectiveFromValidation") + "','" + 
                                                                            this.GetMessage("SOM_EffectiveToValidation") + "','" + 
                                                                            this.GetMessage("SOM_VoidSure") + "')");
            this.btnSaveAndReturn.Attributes.Add("onclick", "return CheckData('" + 
                                                                            this.GetMessage("SOM_FireLinkValidation") + "','" +
                                                                            this.GetMessage("SOM_FireZoneGroupValidation") + "','" + 
                                                                            this.GetMessage("SOM_RowNoValidation") + "','" + 
                                                                            this.GetMessage("SOM_SeqNoValidation") + "','" + 
                                                                            //this.GetMessage("SOM_RowNoValidationNumber") + "','" + 
                                                                            //this.GetMessage("SOM_SeqNoValidationNumber") + "','" + 
                                                                            this.GetMessage("SOM_EffectiveFromValidation") + "','" +
                                                                            this.GetMessage("SOM_EffectiveToValidation") + "','" + 
                                                                            this.GetMessage("SOM_VoidSure") + "')");

            //this.btnSaveAndNew.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");
            //this.btnSaveAndReturn.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");

        }

        void SetLanguage_Click(object sender, EventArgs e)
        {
            SetLanguage();
        }

        private void SetLanguage()
        {
            this.lbl_CodeSetupTitle.Text = this.GetLabel("SOM_CodeSetupFireZoneGroupEntry");
            this.lblTitleFireZoneGroupEntry.Text = this.GetLabel("SOM_FireLinkZoneGroup");
            this.lblFireLink.Text = this.GetLabel("SOM_FireSystem", true);
            this.lblZoneGroup.Text = this.GetLabel("SOM_ZoneGroupLoop", true);
            this.lblDetailLocation.Text = this.GetLabel("SOM_DetailLocation", true);
            this.lblRowNo.Text = this.GetLabel("SOM_RowNo", true);
            this.lblSeq.Text = this.GetLabel("SOM_SeqNo", true);
            this.lblEffectiveFrom.Text = this.GetLabel("SOM_EffectiveFrom", true);
            this.lblEffectiveTo.Text = this.GetLabel("SOM_EffectiveTo", true);

            this.btnSave.Text = this.GetLabel("COM_Save");
            this.btnSaveAndReturn.Text = this.GetLabel("COM_SaveAndReturn");
            this.btnSaveAndNew.Text = this.GetLabel("SOM_SaveNew");
            this.btnCancel.Text = this.GetLabel("COM_Cancel");
            this.chkVoid.Text = this.GetLabel("SOM_Void", true);

            this.txtHideRowNoOverFlow.Text = this.GetMessage("SOM_RowNoOverFlow");
            this.txtHideSeqNoOverFlow.Text = this.GetMessage("SOM_SeqNoOverFlow");
            this.txtHideRowNoSysParameter.Text = MTRC.ODMS.DAL.CommonData.GetNumVal("SOM_CDFireZoneGrp_MaxRowNo").ToString();
            this.txtHideSeqNoSysParameter.Text = MTRC.ODMS.DAL.CommonData.GetNumVal("SOM_CDFireZoneGrp_MaxSeqNo").ToString(); 


            this.txtHideRowNoSysParameterValidation.Text = this.GetMessage("SOM_RowNoSysParameter");
            this.txtHideSeqNoSysParameterValidation.Text = this.GetMessage("SOM_SeqNoSysParameter");
            BindFireLink();
            SetPageLoad();  //move from page load, huangyao 20081208 fixed issue 0785
        }

        private void Clear()
        {
            this.ddlFireLink.SelectedIndex=0;
            this.txtZoneGroup.Text = "";
            this.txtDetailLocation.Text = "";
            this.txtRowNo.Text = "";
            this.txtSeqNo.Text = "";
            this.dtbEffectiveFrom.SetDateTime(DateTime.MinValue);
            this.dtbEffectiveTo.SetDateTime(DateTime.MinValue);
            this.chkVoid.Checked = false;
            this.ddlFireLink.Enabled = true;
        }

        /// <summary>
        /// Bind drop down list FireLink
        /// </summary>
        private void BindFireLink()
        {
            if (ViewState["FireLinkID"].ToString() == string.Empty || this.Request["FireZoneGrpID"] != null)
            {
                //update by gordon 20151026 U247
                //DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDFireZoneGrp.GetCurrentLocationAndUnVoidFireZoneGrp(this.ODMUser.StationID);
                DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDFireZoneGrp.GetCurrentLocationAndUnVoidFireZoneGrp(this.ODMUser.LocationID);
                //this.ddlFireLink.Items.Clear();
                //ddlFireLink.DataSource = dt;
                //ddlFireLink.DataValueField = "FireLinkID";
                //ddlFireLink.DataTextField = "FireLinkCode";
                //ddlFireLink.DataBind();
                //ddlFireLink.Items.Insert(0, LiBlank());
                //ddlFireLink.SelectedIndex = 0;
                //dt.Dispose();
                //dt = null;
                BindListControl(this.ddlFireLink, "FireLinkID", "FireLinkCode", dt,true,"");
            }
        } 

        private void AfterVoid()
        {
            this.ddlFireLink.Enabled = false;
            this.txtZoneGroup.Enabled = false;
            this.txtDetailLocation.Enabled = false;
            this.txtRowNo.Enabled = false;
            this.txtSeqNo.Enabled = false;
            this.dtbEffectiveFrom.Enabled= false;
            this.dtbEffectiveTo.Enabled = false;
            this.chkVoid.Enabled = false;

            this.btnSave.Enabled = false;
            this.btnSaveAndNew.Enabled = false;
            this.btnSaveAndReturn.Enabled = false;
        }

        private int Save(int fireZoneGrpID, int action)
        {
            ViewState["FireLinkID"] = ddlFireLink.SelectedValue;
            return MTRC.ODMS.DAL.SOM.CodeSetup.CDFireZoneGrp.Update(
                                                   fireZoneGrpID,
                                                   int.Parse(this.ddlFireLink.SelectedValue),
                                                   this.txtZoneGroup.Text.Trim(),
                                                   this.txtDetailLocation.Text.Trim(),
                                                   int.Parse(this.txtRowNo.Text.Trim()),
                                                   int.Parse(this.txtSeqNo.Text.Trim()),
                                                   this.dtbEffectiveFrom.GetDateTime(),
                                                   this.dtbEffectiveTo.GetDateTime(),
                                                   DateTime.Now,
                                                   this.chkVoid.Checked,
                                                   this.ODMUser.ID,
                                                   DateTime.Now,
                                                   this.ODMUser.ID,
                                                   action
                                                    );
        }


        private bool CheckUnique(bool insert)
        {
            if (insert == true)
            {
                if (MTRC.ODMS.DAL.SOM.CodeSetup.CDFireZoneGrp.CheckFireZoneGroupUnique(int.Parse(this.ddlFireLink.SelectedValue), this.txtZoneGroup.Text.Trim(), int.Parse(this.txtRowNo.Text.Trim()), int.Parse(this.txtSeqNo.Text.Trim()) ,- 1).Rows.Count > 0)
                {
                    this.Alert(string.Format(this.GetMessage("SOM_FireZoneGroupUnique"),this.txtRowNo.Text.Trim(),this.txtSeqNo.Text.Trim()));
                    return false;
                }
                return true;
            }
            else
            {
                if (MTRC.ODMS.DAL.SOM.CodeSetup.CDFireZoneGrp.CheckFireZoneGroupUnique(int.Parse(this.ddlFireLink.SelectedValue), this.txtZoneGroup.Text.Trim(),int.Parse(this.txtRowNo.Text.Trim()),int.Parse(this.txtSeqNo.Text.Trim()), int.Parse(this.hidID.Value)).Rows.Count>0)
                {
                    this.Alert(string.Format(this.GetMessage("SOM_FireZoneGroupUnique"),this.txtRowNo.Text.Trim(),this.txtSeqNo.Text.Trim()));
                    return false;
                }
                return true;
            }
        }

        private bool CheckData()
        {
            if (InvalidateInputDate(this.dtbEffectiveFrom.GetDateTime(), this.dtbEffectiveTo.GetDateTime()))
            {
                this.Alert(this.GetMessage("COM_StartEndDate"));
                this.dtbEffectiveFrom.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }

        #region
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }

            int id;

            if (this.hidID.Value == String.Empty)
            {
                if (CheckUnique(true))
                {
                    id = Save(0, 1);
                    this.hidID.Value = id.ToString();
                    this.ddlFireLink.Enabled = false;
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                }
            }
            else
            {
                if (CheckUnique(false))
                {
                    id = Save(int.Parse(this.hidID.Value), 2);
                    this.ddlFireLink.Enabled = false;
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                }
            }

            if (this.chkVoid.Checked == true)
            {
                AfterVoid();
            }
        }

        protected void btnSaveAndReturn_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }

            int id;

            if (this.hidID.Value == String.Empty)
            {
                if (CheckUnique(true))
                {
                    id = Save(0, 1);
                }
                else
                {
                    return;
                }
            }
            else
            {

                //if (CheckUnique(true))
                if (CheckUnique(false))  //huangyao 20081208 fixed issue 0784
                {
                    id = Save(int.Parse(this.hidID.Value), 2);
                }
                else
                {
                    return;
                }
            }
            this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));

            string script = "<script language=javascript>function btnChange_Click(){window.location.href='FireZoneGroupList.aspx'} window.setTimeout(btnChange_Click,0);" + "</script>";

            this.Page.RegisterStartupScript("aaa", script);

        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }
            ViewState["FireLinkID"] = string.Empty;
            int id;
            if (this.hidID.Value == String.Empty)
            {
                if (CheckUnique(true))
                {
                    id = Save(0, 1);
                    this.hidID.Value = "";
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                    Clear();
                }
            }
            else
            {
                if (CheckUnique(false))
                {
                    id = Save(int.Parse(this.hidID.Value), 2);
                    this.hidID.Value = "";
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                    Clear();
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("FireZoneGroupList.aspx");
        }
        #endregion
    }
}
